﻿using Shop.Interfaces;
using DAL;
using EntitiesContainer;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing;

namespace Shop
{
    class Presenter
    {
        private IViewMain view;
        private ShopContext data;

        public Presenter(IViewMain view, ShopContext data)
        {
            this.view = view;
            this.data = data;
            this.view.AddButtonClicked += View_AddButtonClicked;
            this.view.CategorySelected += View_CategorySelected;
            this.view.ComboboxProductDropDown += View_ComboboxProductDropDown;
            this.view.ChangeButtonClicked += View_ChangeButtonClicked;
            this.view.DelButtonClicked += View_DelButtonClicked;
            this.view.LogoutClicked += View_LogoutClicked;
            this.view.RowSelected += View_ShowProductInfo;
        }

        private void View_ComboboxProductDropDown()
        {
            SetProductCategoriesToProdcbx(GetAllCategories());
        }

        private void View_ShowProductInfo()
        {
            int prodId = view.ReturnSelectedProductId();
            SetProductInfo(GetProductByProductId(prodId));
        }

        private void View_LogoutClicked()
        {
            throw new NotImplementedException();
        }

        private void View_DelButtonClicked()
        {
            //ищу нужный элемент в контексте и удаляю его
            int id = view.ReturnSelectedProductId();
            Product prod = data.Products.Find(id);
            data.Products.Remove(prod);
            DialogResult res = MessageBox.Show("Do you really want to delete", "Delete", MessageBoxButtons.OKCancel);
            if (res == DialogResult.OK)
            {
                data.SaveChanges();
            }
        }

        private void View_ChangeButtonClicked()
        {
            throw new NotImplementedException();
        }

        private void View_CategorySelected()
        {
            string prodCat = view.ReturnSelectedCategoryTable();
            SetProductsToView(GetProductByCategory(prodCat));
            ClearProductInfo();
        }

        private void View_AddButtonClicked()
        {
            //Product tempProd = new Product() { Price = view.Price, Description = view.Description, Name = view.ProdName, Image = view.ProductImage.,  };
        }

        private ICollection<string> GetAllCategories()
        {
            List<string> temp = new List<string>();
            foreach (ProductCategory category in data.ProductCategories)
            {
                temp.Add(category.Name);
            }
            return temp;
        }

        private void SetProductCategoriesToTablecbx(ICollection<string> listOfProductCategories)
        {
            view.ProductCategoriesFromTable = listOfProductCategories;
        }

        private void SetProductCategoriesToProdcbx(ICollection<string> listOfProductCategories)
        {
            view.SetListCategoriesForProduct(listOfProductCategories);
        }

        private ICollection<Product> GetProductByCategory(string categoryName)
        {
            return data.Products.Where((n) => n.Category.Name == categoryName).ToList();
        }

        private void SetProductsToView(ICollection<Product> products)
        {
            view.tableProducts = products;
        }

        private Product GetProductByProductId(int productId)
        {
            return data.Products.FirstOrDefault((n) => n.ProductId == productId);
        }

        private void SetProductInfo(Product product)
        {
            view.ProdName = product.Name;
            view.Price = product.Price.ToString();
            view.ProductCategoryFromProdInfo = product.Category.Name;
            view.Description = product.Description;
            if (product.Image == null)
            {
                view.ProductImage = Image.FromFile(Application.StartupPath + "\\images\\logo.png");
            }
            else
            {
                view.ProductImage = Image.FromFile(product.Image);
            }
        }

        private void ClearProductInfo()
        {
            view.ProdName = string.Empty;
            view.Price = string.Empty;
            view.ProductCategoryFromProdInfo = string.Empty;
            view.Description = string.Empty;
            view.ProductImage = null;
        }
        public void Run()
        {
            SetProductCategoriesToTablecbx(GetAllCategories());
            this.view.Show();
        }
    }
}
